package com.feicheng.lanniao.controller.pc;

import java.time.LocalDateTime;

import com.feicheng.lanniao.anno.StaticSelectCountOfCarRentalInfo;
import com.feicheng.lanniao.exception.SystemException;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.feicheng.lanniao.entity.Car;
import com.feicheng.lanniao.entity.CarBuyInfo;
import com.feicheng.lanniao.entity.CarRentInfo;
import com.feicheng.lanniao.entity.CarRentalClientVo;
import com.feicheng.lanniao.entity.CarRentalInfo;
import com.feicheng.lanniao.entity.CarRentalOwner;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.ICarBuyClientVoService;
import com.feicheng.lanniao.service.ICarRentClientVoService;
import com.feicheng.lanniao.service.ICarRentalClientVoService;
import com.feicheng.lanniao.service.ICarRentalInfoService;
import com.feicheng.lanniao.service.ICarRentalOwnerService;
import com.feicheng.lanniao.service.ICarService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author feichengTech
 * @since 2021-09-02
 */
@RestController
@RequestMapping("/lanniao-user/car-rental-info")
public class CarRentalPcClientVoController {
	@Autowired
	ICarRentalClientVoService ICarRentalClientVoService;
	@GetMapping("/page")
	public ResultInfo rentCarList(Integer clientId,
								String searchKey,
								Integer conditionId,
								Integer mileageId,
								Integer brandId,
								Integer rentalPriceId,
								String type,
								String ownerShip,
								@RequestParam(required = false,defaultValue = "1") Integer currentPage,
								@RequestParam(required = false,defaultValue = "10") Integer pageSize) {

		return ICarRentalClientVoService.rentCarList(clientId,searchKey,conditionId, mileageId, brandId,rentalPriceId,type,ownerShip ,currentPage, pageSize);
	}
	//@StaticSelectCountOfCarRentalInfo //切面
	@GetMapping("/info")
	public ResultInfo getOneCr(Integer carId) {
		return ICarRentalClientVoService.getOneRentalBuyInfo(carId);
	}
	@PutMapping("/info")
	public ResultInfo editCr(CarRentalClientVo carRentalInfo) {
		return ICarRentalClientVoService.editOneCarRentalInfo(carRentalInfo);
	}
	@DeleteMapping("/info")
	public ResultInfo deleteCar(Integer carRentalInfoId) {
		return ICarRentalClientVoService.delOneCar(carRentalInfoId);
	}
	@Autowired
	private ICarRentalInfoService icarRentalInfoService;
	@Autowired
	private ICarService iCarService;
	@Autowired
	private ICarRentalOwnerService carRentalOwnerService;
	@PostMapping("/info")
	public ResultInfo addResultInfo(
			Integer carCondition,  // not null
			Integer mileage,     // not null
			String ownerShip,
			Integer brandId, // not null
			String type,      //not null
			String imgUrl,
			Integer rentalPrice,
			Integer clientId,  // not null
			String tel,
			String wxNumber
			) {
		Car car = new Car();
		car.setCarCondition(carCondition);
		car.setMileage(mileage);
		car.setOwnerShip(ownerShip);
		car.setBrandId(brandId);
		car.setType(type);
		car.setImgUrl(imgUrl);
		car.setRentalPrice(rentalPrice);
		iCarService.save(car);

		//创建CarRentalInfo对象
		CarRentalInfo carRentalInfo = new CarRentalInfo();
		carRentalInfo.setCarId(car.getCarId());
		carRentalInfo.setRentalPrice(car.getRentalPrice());
		carRentalInfo.setTel(tel);
		carRentalInfo.setWxNumber(wxNumber);
		System.out.println();
		icarRentalInfoService.save(carRentalInfo);
		//创建中间表对象
		CarRentalOwner carRentalOwner = new CarRentalOwner();
		if(clientId == null){
			throw new SystemException("ClientId不能为空");
		}
		carRentalOwner.setClientId(clientId);
		carRentalOwner.setCarRentalInfoId(carRentalInfo.getCarRentalInfoId());
		System.out.println(carRentalInfo.getCarRentalInfoId());
		carRentalOwnerService.save(carRentalOwner);
		return ResultInfo.success();
	}
}
